package com.hskn.hss.module.tlbind.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlbind.mapper.TlBindMapper;
import com.hskn.hss.module.tlbind.service.ITlBindService;
import com.hskn.hss.module.tlcar.mapper.TlCarMapper;
import com.hskn.hss.module.tlemployee.mapper.TlEmployeeMapper;
import com.hskn.hss.module.tlequip.mapper.TlEquipMapper;
import com.hskn.hss.module.tlfacilities.mapper.TlFacilitiesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TlBindServiceImpl implements ITlBindService {
    @Autowired
    TlEquipMapper tlEquipMapper;
    @Autowired
    TlFacilitiesMapper tlFacilitiesMapper;
    @Autowired
    TlCarMapper tlCarMapper;
    @Autowired
    TlEmployeeMapper tlEmployeeMapper;
    @Autowired
    TlBindMapper tlBindMapper;

//    @Override
//    @Transactional
//    public AjaxResult bind(JSONObject params) throws HssException {
//        TlBind tlBind = params.getObject("data" , TlBind.class );
//        String equipids = tlBind.getEquipid();
//
//        for (String equipid:   equipids.split(",")   ) {
//            TlEquip tlEquip = tlEquipMapper.selectById(equipid);
//            if(tlBind.getStatus()!=null && tlBind.getStatus().equals("1")){
//                tlEquip.setBinddeptid(tlBind.getDeptid());
//                tlEquip.setBindobjid(tlBind.getId());
//                tlEquip.setBindtype(tlBind.getType());
//                tlEquip.setBindstatus(tlBind.getStatus());
//                tlEquip.setBindtime(new Date());
//                tlEquip.setBindobjname(tlBind.getObjname());
//                tlEquip.setBindtypeitem(tlBind.getItemid());
//            } if(tlBind.getStatus()!=null && tlBind.getStatus().equals("2")){
//
//                if (tlBind.getType().equals("car")){
//                    TlCar tlCar = tlCarMapper.selectById(tlEquip.getBindobjid());
//                    tlCar.setBindstatus("0");
//                    String bindfacids =  tlCar.getBindfaclids();
//                    String objname = tlCar.getBandname();
//
//                    String[] objnameArry = objname.split(",");
//                    String replacename ="";
//                    for (String s : objnameArry){
//                        if (s.contains(tlEquip.getEquipcode())){
//                            replacename = s;
//                        }
//                    }
//                    if(bindfacids.contains(","+tlEquip.getId())){
//                        bindfacids=bindfacids.replace(","+tlEquip.getId(),"");
//                        objname = objname.replace(","+replacename,"");
//                    }else if(bindfacids.contains(tlEquip.getId()+",")){
//                        bindfacids=bindfacids.replace(tlEquip.getId()+",","");
//                        objname = objname.replace(replacename+",","");
//                    }else if(bindfacids.contains(tlEquip.getId())){
//                        bindfacids=bindfacids.replace(tlEquip.getId(),"");
//                        objname = objname.replace(replacename,"");
//                    }
//
//                    tlCar.setBindfaclids(bindfacids);
//                    tlCar.setBandname(objname);
//                    tlCarMapper.updateById(tlCar);
//
//                }else  if (tlBind.getType().equals("emp")){
//                    TlEmployee tlEmployee = tlEmployeeMapper.selectById(tlEquip.getBindobjid());
//                    tlEmployee.setBindstatus("0");
//                    String bindfacids =  tlEmployee.getBindfaclids();
//                    String objname = tlEmployee.getBindfaclname();
//
//                    String[] objnameArry = objname.split(",");
//                    String replacename ="";
//                    for (String s : objnameArry){
//                        if (s.contains(tlEquip.getEquipcode())){
//                            replacename = s;
//                        }
//                    }
//
//                    if(bindfacids.contains(","+tlEquip.getId())){
//                        bindfacids=bindfacids.replace(","+tlEquip.getId(),"");
//                        objname = objname.replace(","+replacename,"");
//                    }else if(bindfacids.contains(tlEquip.getId()+",")){
//                        bindfacids=bindfacids.replace(tlEquip.getId()+",","");
//                        objname = objname.replace(replacename+",","");
//                    }else if(bindfacids.contains(tlEquip.getId())){
//                        bindfacids=bindfacids.replace(tlEquip.getId(),"");
//                        objname = objname.replace(replacename,"");
//                    }
//
//                    tlEmployee.setBindfaclids(bindfacids);
//                    tlEmployee.setBindfaclname(objname);
//                    tlEmployeeMapper.updateById(tlEmployee);
//                }else {
//                    TlFacilities tlFacilities = tlFacilitiesMapper.selectById(tlEquip.getBindobjid());
//                    tlFacilities.setBindstatus("0");
//                    String bindfacids =  tlFacilities.getBindequipids();
//                    String objname = tlFacilities.getBindobjname();
//
//                    String[] objnameArry = objname.split(",");
//                    String replacename ="";
//                    for (String s : objnameArry){
//                        if (s.contains(tlEquip.getEquipcode())){
//                            replacename = s;
//                        }
//                    }
//                    if(bindfacids.contains(","+tlEquip.getId())){
//                        bindfacids=bindfacids.replace(","+tlEquip.getId(),"");
//                        objname = objname.replace(","+replacename,"");
//                    }else if(bindfacids.contains(tlEquip.getId()+",")){
//                        bindfacids=bindfacids.replace(tlEquip.getId()+",","");
//                        objname = objname.replace(replacename+",","");
//                    }else if(bindfacids.contains(tlEquip.getId())){
//                        bindfacids=bindfacids.replace(tlEquip.getId(),"");
//                        objname = objname.replace(replacename,"");
//                    }
//                    tlFacilities.setBindequipids(bindfacids);
//                    tlFacilities.setBindobjname(objname);
//                    tlFacilitiesMapper.updateById(tlFacilities);
//                }
//                tlEquip.setBinddeptid(tlBind.getDeptid());
//                tlEquip.setBindobjid(tlBind.getId());
//                tlEquip.setBindtype(tlBind.getType());
//                tlEquip.setBindstatus(tlBind.getStatus());
//                tlEquip.setBindtime(new Date());
//                tlEquip.setBindobjname(tlBind.getObjname());
//                tlEquip.setBindtypeitem(tlBind.getItemid());
//            }else if(tlBind.getStatus()!=null && tlBind.getStatus().equals("0")) {
//                tlEquip.setBinddeptid("");
//                tlEquip.setBindobjid("");
//                tlEquip.setBindtype("");
//                tlEquip.setBindstatus(tlBind.getStatus());
//                tlEquip.setBindtime(new Date());
//                tlEquip.setBindobjname("");
//                tlEquip.setBindtypeitem("");
//            }
//
//            tlEquipMapper.updateById(tlEquip);
//        }
//
//
//        if (tlBind.getType().equals("car")){
//            TlCar tlCar = tlCarMapper.selectById(tlBind.getId());
//            tlCar.setBindstatus(tlBind.getStatus());
//            String bindfacids =  tlCar.getBindfaclids();
//            String objname = tlCar.getBandname();
//            if ( bindfacids == null || bindfacids.equals("")){
//                bindfacids = tlBind.getEquipid();
//                objname = tlBind.getEquiptypename();
//            }else if(bindfacids.contains(","+tlBind.getEquipid())){
//                bindfacids=bindfacids.replace(","+tlBind.getEquipid(),"");
//                objname = objname.replace(","+tlBind.getEquiptypename(),"");
//            }else if(bindfacids.contains(tlBind.getEquipid()+",")){
//                bindfacids=bindfacids.replace(tlBind.getEquipid()+",","");
//                objname = objname.replace(tlBind.getEquiptypename()+",","");
//            }else if(bindfacids.contains(tlBind.getEquipid())){
//                bindfacids=bindfacids.replace(tlBind.getEquipid(),"");
//                objname = objname.replace(tlBind.getEquiptypename(),"");
//            }else {
//                bindfacids+=","+tlBind.getEquipid();
//                objname += ","+tlBind.getEquiptypename();
//            }
//            tlCar.setBindfaclids(bindfacids);
//            tlCar.setBandname(objname);
//            tlCarMapper.updateById(tlCar);
//
//        }else  if (tlBind.getType().equals("emp")){
//            TlEmployee tlEmployee = tlEmployeeMapper.selectById(tlBind.getId());
//            tlEmployee.setBindstatus(tlBind.getStatus());
//            String bindfacids =  tlEmployee.getBindfaclids();
//            String objname = tlBind.getEquiptypename();
//            if ( bindfacids == null || bindfacids.equals("")){
//                bindfacids = tlBind.getEquipid();
//                objname = tlBind.getEquiptypename();
//            }else if(bindfacids.contains(","+tlBind.getEquipid())){
//                bindfacids=bindfacids.replace(","+tlBind.getEquipid(),"");
//                objname = objname.replace(","+tlBind.getEquiptypename(),"");
//            }else if(bindfacids.contains(tlBind.getEquipid()+",")){
//                bindfacids=bindfacids.replace(tlBind.getEquipid()+",","");
//                objname = objname.replace(tlBind.getEquiptypename()+",","");
//            }else if(bindfacids.contains(tlBind.getEquipid())){
//                bindfacids=bindfacids.replace(tlBind.getEquipid(),"");
//                objname = objname.replace(tlBind.getEquiptypename(),"");
//            }else {
//                bindfacids+=","+tlBind.getEquipid();
//                objname += ","+tlBind.getEquiptypename();
//            }
//            tlEmployee.setBindfaclids(bindfacids);
//            tlEmployee.setBindfaclname(objname);
//            tlEmployeeMapper.updateById(tlEmployee);
//        }else {
//            TlFacilities tlFacilities = tlFacilitiesMapper.selectById(tlBind.getId());
//            tlFacilities.setBindstatus(tlBind.getStatus());
//            String bindfacids =  tlFacilities.getBindequipids();
//            String objname = tlFacilities.getBindobjname();
//            if ( bindfacids == null || bindfacids.equals("")){
//                bindfacids = tlBind.getEquipid();
//                objname = tlBind.getEquiptypename();
//            }else if(bindfacids.contains(","+tlBind.getEquipid())){
//                bindfacids=bindfacids.replace(","+tlBind.getEquipid(),"");
//                objname = objname.replace(","+tlBind.getEquiptypename(),"");
//            }else if(bindfacids.contains(tlBind.getEquipid()+",")){
//                bindfacids=bindfacids.replace(tlBind.getEquipid()+",","");
//                objname = objname.replace(tlBind.getEquiptypename()+",","");
//            }else if(bindfacids.contains(tlBind.getEquipid())){
//                bindfacids=bindfacids.replace(tlBind.getEquipid(),"");
//                objname = objname.replace(tlBind.getEquiptypename(),"");
//            }else {
//                bindfacids+=","+tlBind.getEquipid();
//                objname += ","+tlBind.getEquiptypename();
//            }
//            tlFacilities.setBindequipids(bindfacids);
//            tlFacilities.setBindobjname(objname);
//            tlFacilitiesMapper.updateById(tlFacilities);
//        }
//
//        return AjaxResult.success("操作成功");
//    }

    @Override
    public List getobjList(JSONObject params) throws HssException {
        Map<String, Object> map = new HashMap();

        map.put("objname", params.getString("objname"));
        map.put("type", params.getString("type"));
        map.put("status", params.getString("status"));
        map.put("item", params.getString("item"));

        List resultList = tlBindMapper.getobjList(map);
        return resultList;
    }

    @Override
    public List getobjSelectList(JSONObject params) throws HssException {
        Map<String, Object> map = new HashMap();
        map.put("type", params.getString("type"));
        map.put("status", params.getString("status"));
        map.put("item", params.getString("item"));
        List resultList = tlBindMapper.getobjSelectList(map);
        return resultList;
    }


}
